//
//  PointProgressView.m
//  YouHui
//
//  Created by  on 11-11-29.
//  Copyright (c) 2011年 netease. All rights reserved.
//

#import "ZBPointProgressView.h"

@implementation ZBPointProgressView

@synthesize pointCount=_pointCount;
@synthesize progress=_progress;
@synthesize pointSpace=_pointSpace;

- (id)initWithFrame:(CGRect)frame {
    self =[super initWithFrame:frame];
    if (self) {
        _pointCount = 10;
        _progress = 5;
        _pointSpace = 5.0;
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
        self.userInteractionEnabled = NO;
    }
    return self;
}

- (void)setPointCount:(NSUInteger)pointCount {
    if  (_pointCount != pointCount) {
        _pointCount = pointCount;
        [self setNeedsDisplay];
    }    
}

- (void)setProgress:(NSUInteger)progress {
    if  (_progress != progress) {
        _progress = progress;
        [self setNeedsDisplay];
    }
}

- (void)setPointSpace:(CGFloat)pointSpace {
    if  (_pointSpace != pointSpace) {
        _pointSpace = pointSpace;
        [self setNeedsDisplay];
    }
}

-(void)fillRoundRect:(CGContextRef)context rect:(CGRect)rect radius:(CGFloat)radius
{
	CGPoint leftTop = rect.origin;
	CGPoint rightTop = CGPointMake(leftTop.x+rect.size.width,leftTop.y);
	CGPoint rightBottom = CGPointMake(rightTop.x, rightTop.y+rect.size.height);
	CGPoint leftBottom = CGPointMake(leftTop.x, leftTop.y+rect.size.height);
	
	CGContextMoveToPoint(context, leftTop.x, leftTop.y+radius);
	CGContextAddArcToPoint(context, leftTop.x, leftTop.y, leftTop.x+radius, leftTop.y, radius);
	CGContextAddArcToPoint(context, rightTop.x, rightTop.y, rightTop.x, rightTop.y+radius,radius);
	CGContextAddArcToPoint(context, rightBottom.x, rightBottom.y, rightBottom.x-radius, rightBottom.y, radius);
	CGContextAddArcToPoint(context, leftBottom.x,leftBottom.y,leftBottom.x,leftBottom.y-radius,radius);
	CGContextClosePath(context);
	
	CGContextFillPath(context);
}

-(void)drawRect:(CGRect)rect
{
    rect = self.bounds;
    
	CGContextRef context = UIGraphicsGetCurrentContext();
	UIColor *darkColor = [UIColor colorWithRed:59/255.0f green:56/255.0f blue:50/255.0f alpha:1.0f];
    
	CGFloat radius = 4;
	CGPoint initPoint = CGPointMake((rect.size.width-(radius*2*self.pointCount)-(self.pointCount-1)*_pointSpace)/2,(rect.size.height-radius*2)/2);
	
	for (int i=0; i<self.pointCount; i++) {
        [[UIColor whiteColor] setFill];
		CGRect smallRect = CGRectMake(initPoint.x, initPoint.y, radius*2, radius*2);
		CGContextFillEllipseInRect(context,smallRect);		
		
		if (i==_progress) {
            [darkColor setFill];
            CGContextFillEllipseInRect(context,smallRect);
		}		
		initPoint.x += 2*radius+_pointSpace;
	}	
}
@end

